﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Longbow.Config;
using System.Reflection;
using System.IO;
using System.Collections;

namespace Longbow.Config
{
	public class MemConfig : IConfig
	{

		private bool loaded = false;

		private string[] assemblyModulePaths = null;

		private string[] nativeModulePaths = null;

		private ModuleLoadPolicy loadPolicy = null;

		#region IConfig members

		public bool Loaded
		{
			get { return loaded; }
		}

		public bool Load()
		{
			string appPath = Assembly.GetExecutingAssembly().Location;
			appPath = appPath.Substring(0, appPath.LastIndexOf('\\'));
			assemblyModulePaths = new string[] { (appPath + @"\assembly") };
			nativeModulePaths = new string[] { (appPath + @"\native") };
			loaded = true;

			return true;
		}

		public string[] GetModulePaths(ModuleType moduleType)
		{
			if (moduleType == ModuleType.AssemblyModule)
				return assemblyModulePaths;
			else if(moduleType == ModuleType.NativeModule)
				return nativeModulePaths;
			return null;
		}

		public string[] GetModuleFileNames(string path, ModuleType moduleType)
		{
			if (Directory.Exists(path))
			{
				string[] result = Directory.GetFiles(path);
				if (this.loadPolicy != null)
				{
					List<string> al = new List<string>(result.Length);
					foreach (string fileName in result)
					{
						if (loadPolicy(fileName))
						{
							al.Add(fileName);
						}
					}
					result = al.ToArray();
					al.Clear();
					return result;
				}
				else
				{
					return result;
				}
			}
			return null;
		}



		public void  SetLoadPolicy(ModuleLoadPolicy loadPolicy)
		{
 			this.loadPolicy = loadPolicy;
		}

		public string GetModuleProperty(string fileName, string propertyName)
		{
			if (propertyName.Equals("load.policy"))
				return "LoadNow";
			return "";
		}

		#endregion
}
}
